/**
 * @Description:    各种工具类
 * @Author:         TSY
 * @CreateDate:     2018/6/9 13:28
 */

/**
 * 判断运行平台
 * @returns {string} 1：安卓  2：IOS  3：PC
 */
export const checkPlatform = () => {
    if (/android/i.test(navigator.userAgent)) {
        return '1'
    } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return '2'
    } else {
        return '3'
    }
}


/**
 *
 * @param func  防抖函数
 * @param wait 等待时间
 * @param immediate
 * @returns {Function}
 */
export let debounce = (func, wait, immediate) => {
    let timeout;
    return function () {
        let context = this, args = arguments;
        let later = function () {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        let callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

/**
 *
 * @param fn  节流函数
 * @param delayTime  等待时间
 * @returns {Function}
 */
export let throttle = (fn, delayTime) => {
    let flag: number;
    return function () {
        let context = this, args = arguments;
        if (!flag) {
            flag = setTimeout(function () {
                fn.apply(context, args);
                flag = 0;
            }, delayTime);
        }
    }
}

export let Fragment = function (this:any, parameters: { v0: any, v1: any, v2: any, image: any } ) {
    let {v0, v1, v2, image} = parameters;
    this.v0 = v0;
    this.v1 = v1;
    this.v2 = v2;
    this.computeBoundingBox();
    this.computeCentroid();
    this.createCanvas();
    this.clip(image);
};
Fragment.prototype = {
    computeBoundingBox(this:any) {
        var xMin = Math.min(this.v0[0], this.v1[0], this.v2[0]), xMax = Math.max(this.v0[0], this.v1[0], this.v2[0]), yMin = Math.min(this.v0[1], this.v1[1], this.v2[1]), yMax = Math.max(this.v0[1], this.v1[1], this.v2[1]);
        this.box = {
            x: xMin,
            y: yMin,
            w: xMax - xMin,
            h: yMax - yMin
        };
    },
    computeCentroid(this:any) {
        var x = (this.v0[0] + this.v1[0] + this.v2[0]) / 3, y = (this.v0[1] + this.v1[1] + this.v2[1]) / 3;
        this.centroid = [
            x,
            y
        ];
    },
    createCanvas(this:any) {
        this.canvas = document.createElement('canvas');
        this.canvas.width = this.box.w;
        this.canvas.height = this.box.h;
        this.canvas.style.width = this.box.w + 'px';
        this.canvas.style.height = this.box.h + 'px';
        this.canvas.style.left = this.box.x + 'px';
        this.canvas.style.top = this.box.y + 'px';
        this.ctx = this.canvas.getContext('2d');
    },
    clip(this:any,image: object) {
        this.ctx.translate(-this.box.x, -this.box.y);
        this.ctx.beginPath();
        this.ctx.moveTo(this.v0[0], this.v0[1]);
        this.ctx.lineTo(this.v1[0], this.v1[1]);
        this.ctx.lineTo(this.v2[0], this.v2[1]);
        this.ctx.closePath();
        this.ctx.clip();
        this.ctx.drawImage(image, 0, 0);
    }
};
